package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.File;
import com.itheima.service.FileService;
import com.itheima.utils.IdWorker;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @author xuchangshuo
 * @date 2020/8/6 19:27
 */
@RestController
@CrossOrigin
@RequestMapping("/file")
public class FileController {
    //文件路径
    private final static String FILE_URL = "fileurl";
    //七牛云存储路径
    private final static String QINIU_URL = "http://qen2m0szi.bkt.clouddn.com/";
    @Autowired
    private FileService fileService;

    @Autowired
    private IdWorker idWorker;

    //获取图片信息
    @RequestMapping("/getPicture")
    public Result getPicture(@RequestParam String fileIds, @RequestParam String code) {
        ArrayList<File> list = new ArrayList<>();
        if (fileIds != null) {
            //分割文件名
            String[] fileId = fileIds.split(code);
            for (String uid : fileId) {
                File file = fileService.getPicture(uid);
                list.add(file);
            }
        }
        return new Result("success", list);
    }

    //图片上传
    @RequestMapping("/pictures")
    public Result upload(String userUid,
                         String sysUserId,
                         String projectName,
                         String sortName,
                         MultipartFile[] filedatas) {
        //创建文件对象
        File file = new File();
        if (filedatas != null) {
            for (MultipartFile filedata : filedatas) {
                try {
                    //获取源文件名
                    String originalFilename = filedata.getOriginalFilename();
                    // 获取最后一个.的索引
                    int index = originalFilename.lastIndexOf(".");
                    //获取文件扩展名    截取点后面的内容
                    String expName = originalFilename.substring(index);
                    //获取文件大小
                    long size = filedata.getSize();
                    // 截取最后一个点和后面的文件格式,获取存储得文件名
                    String fileName = UUID.randomUUID().toString() + originalFilename.substring(index - 1);
                    QiniuUtils.upload2Qiniu(filedata.getBytes(), fileName);

                    file.setUid(idWorker.nextId() + "");
                    file.setFileOldName(originalFilename);
                    file.setPicName(fileName);
                    file.setPicUrl(FILE_URL + originalFilename);
                    file.setPicExpandedName(expName);
                    file.setFileSize(size);
                    file.setAdminUid(sysUserId);
                    file.setUserUid(userUid);
                    file.setStatus(1);
                    file.setCreateTime(new Date());
                    file.setUpdateTime(new Date());
                    file.setQiNiuUrl(QINIU_URL + fileName);
                    fileService.upload(file, projectName, sortName);
                } catch (IOException e) {
                    e.printStackTrace();
                    return new Result("faild", "文件上传失败");
                }
            }
        }

        return new Result("success", "文件上传成功");

    }

}
